<%@ include file="css/header.jsp" %>
<title>Manage groups</title>
<%
String userId;
String manageGroups;
if((String)session.getAttribute("userId")==null||((String)session.getAttribute("userId")).trim().length() == 0) userId = "null";
else userId = (String)session.getAttribute("userId");
if((String)session.getAttribute("manageGroups")==null||((String)session.getAttribute("manageGroups")).trim().length() == 0) manageGroups = "null";
else manageGroups = (String)session.getAttribute("manageGroups");
	if(!(userId.equals("null"))){
		if(manageGroups.equals("1")){
%>

<script type="text/javascript">
	var jqueryOption = {};
	jqueryOption.grdRowNum = 50;
	jqueryOption.grdRowList = [50, 100, 200];
	jqueryOption.grdJsonReader = {
	    root: "rows",
	    page: "page",
	    total: "total",
	    records: "records",
	    repeatitems: true, //if 'false' will chage json reader format to 'name' require
	    //cell: "cell",
	    id: "0" //id: "id"
	};

$(document).ready(function() {
	$('#mnu li').hover(
	        function () {
	            //show its submenu
	            $('ul', this).slideDown(100);
	        }, 
	        function () {
	            //hide its submenu
	            $('ul', this).slideUp(100);         
	        }
	    );
    
	$("#btnAdd").click(function(){
		window.location.href = "AddGroup.jsp";
	    return false;
	});
	$("#btnClear").click(function(){
		var gridObj = $("#grdPermission");
        var ids = gridObj.jqGrid('getDataIDs');
        var contentValue;
        for (var i = 0; i < ids.length; i++) {
            var rowid = ids[i];
            var tcdTracking = 1 ;
            var rdTracking = 0;
            var tcdBilling = 0;
            var rdBilling = 0;
            var manageUsers = 0;
            var manageGroups = 0;
            var userLog = 0;
            var reTCDTCD = 0;
            var reJPMCTCD = 0;
            var reRDRD = 0;
            var reJPMCRD = 0;
            gridObj.jqGrid('setRowData',rowid,{tcdTracking:tcdTracking});
            gridObj.jqGrid('setRowData',rowid,{rdTracking:rdTracking});
            gridObj.jqGrid('setRowData',rowid,{tcdBilling:tcdBilling});
            gridObj.jqGrid('setRowData',rowid,{rdBilling:rdBilling});
            gridObj.jqGrid('setRowData',rowid,{manageUsers:manageUsers});
            gridObj.jqGrid('setRowData',rowid,{manageGroups:manageGroups});
            gridObj.jqGrid('setRowData',rowid,{userLog:userLog});
            gridObj.jqGrid('setRowData',rowid,{reTCDTCD:reTCDTCD});
            gridObj.jqGrid('setRowData',rowid,{reJPMCTCD:reJPMCTCD});
            gridObj.jqGrid('setRowData',rowid,{reRDRD:reRDRD});
            gridObj.jqGrid('setRowData',rowid,{reJPMCRD:reJPMCRD});
        }
	    return false;
	});

	$("#btnSave").click(function(){
		 var gridObj = $("#grdPermission");
		 var gridData = $("#grdPermission").jqGrid('getRowData');
		  var postData = JSON.stringify(gridData);
		  $.ajax({
              type: 'POST',
              url: '<%=request.getContextPath()%>/ManageGroupServlet/SavePermission',
              data: ({data: postData}),
              dataType: 'json',
              success: function(data){
                  $.each(data, function(key, value) {
                      //Validate is error send?
                      if (key == 'error') {
                          //alert(value);
                          $.msgbox(value , {
                              type : 'error'
                          }, function(buttonPressed) {
                              // your code here
                          });
                      } else { //No Error
                          $.msgbox('Save complete.', {
                              type : 'info'
                          }, function(buttonPressed) {
                        	  bindGrdPermission(1);
                          });
                      }
                      return false; //break each loop
                  });
              },
              error: function(){
                  $.msgbox('<fmt:message key="Msg_Error" />', {
                      type : 'error'
                  }, function(buttonPressed) {
                      // your code here
                  });
              }
          });
		 
	    return false;
	});
       
    $("#grdPermission").jqGrid({
        	 url:'<%=request.getContextPath()%>/ManageGroupServlet/DisplayPermission',
        	datatype: 'json',
            mtype: 'POST',
            colNames:['id','Group','TCD Tracking','RD Tracking','TCD Billing','RD Billing','Manage users','Manage groups','User log','Resend TCD(TCD)','Resend JPMC(TCD)','Resend RD(RD)','Resemd JPMC(RD)','Delete'],
            colModel:[
                      {name:'id', index:'id', sortable:false, hidden:true},
                      {name:'name', index:'name', width:12, sortable:false},
                      {name:'tcdTracking', index:'tcdTracking', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:true}},
                      {name:'rdTracking',index:'rdTracking', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'tcdBilling', index:'tcdBilling', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'rdBilling', index:'rdBilling', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'manageUsers',index:'manageUsers', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'manageGroups', index:'manageGroups', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'userLog',index:'userLog', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'reTCDTCD', index:'reTCDTCD', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'reJPMCTCD', index:'reJPMCTCD', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'reRDRD', index:'reRDRD', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      {name:'reJPMCRD', index:'reJPMCRD', align:'center', width:8, sortable:false,editable: true, edittype:"checkbox", editoptions:{value:'1:0'}, formatter:"checkbox", formatoptions:{disabled:false}},
                      
                      {name:'deleteGroup',align:'center', index:'deleteGroup', width:8,sortable:false}
                      ],
            postData: {
            },
            rowNum: jqueryOption.grdRowNum, //50
            rowList: jqueryOption.grdRowList, //[50, 100, 200]
            autowidth: true, //Equal Container !Not change when resize window
            height: -1, //Auto height
            scrollOffset: 1, //Hide scroll bar
            pager: '#pagerPermission',
            rownumbers: true,
            viewrecords: true,
            sortname: 'id',
            sortorder: "desc",
            cache: false,
            caption: 'Permission',
            emptyrecords: 'No result',
            jsonReader : jqueryOption.grdJsonReader,
            onPaging: function(pgButton){ //pgButton : check which button click
            	bindGrdPermission($("#grdPermission").getGridParam('page'));
            },
            onSortCol: function(index, iCol, sortorder){
            	bindGrdPermission(1);
            },
            loadError: function(xhr, status, error) {
            	
            },
            loadComplete: function(data) {
            	 try {
 					if(data.error != null){
 	                	$.msgbox(data.error, {
 	                        type : 'error'
 	                    }, function(buttonPressed) {
 	                    
 	                    });
 					}
 				} catch (e) {
 					
 				}
            },gridComplete: function() {
                var gridObj = $("#grdPermission");
                var ids = gridObj.jqGrid('getDataIDs');
                var contentValue;
                for (var i = 0; i < ids.length; i++) {
                    var rowid = ids[i];
                    var deleteGroup = '<a href="javascript:void(0)" onClick="doDelete(\''+rowid+ '\'); return false">Delete</a>';
                    gridObj.jqGrid('setRowData',rowid,{deleteGroup:deleteGroup});
                }
            }
        });
    });

	function bindGrdPermission(ipage){
	   $("#grdPermission").jqGrid('setGridParam'
	    ,{url:"<%=request.getContextPath()%>/ManageGroupServlet/DisplayPermission"
	        ,page: ipage}).trigger("reloadGrid");
	}

	function doDelete(id){
		$.msgbox('Confirm Delete or not ?', {
	        type : 'confirm',
	        buttons : [
	            {type: 'submit', value:'Yes'},
	            {type: 'cancel', value:'No'}
	        ]
	    }, function(buttonPressed) {
	        if(buttonPressed == 'Yes'){
	            $.ajax({
	                type: 'POST',
	                url: '<%=request.getContextPath()%>/ManageGroupServlet/DeleteGroup?id='+id,
	                dataType: 'json',
	                success: function(data){
	                    $.each(data, function(key, value) {
	                        //Validate is error send?
	                        if (key == 'error') {
	                            $.msgbox(value , {
	                                type : 'error'
	                            }, function(buttonPressed) {
	                                // your code here
	                            });
	                        } else { //No Error
	                        	bindGrdPermission(1)
	                        }
	                        return false; //break each loop
	                    });
	                },
	                error: function(){
	                    $.msgbox('Error', {
	                        type : 'error'
	                    }, function(buttonPressed) {
	                        // your code here
	                    });
	                }
	            });
	        }
	    });
	}
</script>


<body>
<div id="jp-main">
        <div class="jp-sheet">
            <div class="jp-sheet-tl"></div>
            <div class="jp-sheet-tr"></div>
            <div class="jp-sheet-bl"></div>
            <div class="jp-sheet-br"></div>
            <div class="jp-sheet-tc"></div>
            <div class="jp-sheet-bc"></div>
            <div class="jp-sheet-cl"></div>
            <div class="jp-sheet-cr"></div>
            <div class="jp-sheet-cc"></div>
            <div class="jp-sheet-body">
                <div class="jp-header">
                    <div class="jp-header-center">
                        <div class="jp-header-png"></div>
                        <div class="jp-header-jpeg"></div>
                    </div>
                        <div class="jp-header-fluid-object"></div>
                </div>
                <div class="jp-nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="jp-menu">
                		
                			<%if (session.getAttribute("tcdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServlet" ><span class="l"></span><span class="r"></span><span class="t">TCD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServletRD"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("tcdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="TCDBillingServlet"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">TCD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="RDBilling"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (!(userId.equals("0"))){%>
					    	  <li>
					    	  	<a href="EditProfile.jsp"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">Edit profile</span></a>                		
                			  </li>
					      	<%} %>
                			
           		  		<li>
                			<a href="#" class="active"><span class="l"></span><span class="r"></span><span class="t">User Management</span></a>
							<ul>
								<%if (session.getAttribute("manageUsers").equals("1")){%>
						    		<li><a href="ManageAdminServlet">Manage users</a></li>
						      	<%} %>
						      	<%if (session.getAttribute("manageGroups").equals("1")){%>
					    			<li><a href="ManageGroupServlet">Manage groups</a></li>
					      		<%} %>
					      		<%if (session.getAttribute("userLog").equals("1")){%>
					    			<li><a href="UserLogServlet">User log</a></li>
					      		<%} %>
           					</ul>
               		  	</li>		
           		  	  	<li>
                			<a href="Logout"><span class="l"></span><span class="r"></span><span class="t">Logout</span></a>                		
                	  	</li>
               	  </ul>
                </div>
                <div class="jp-content-layout">
                  <div class="jp-post-body">
                     <h2 class="jp-postheader">Manage groups</h2>
                      <div class="jp-postcontent">
                      <div id="ManageGroup">
                      <fieldset id="fsManageGroup">
			            <legend class="pageTitleForm"><h3>Manage groups</h3></legend>
			            <div>&nbsp;<div>
			            <div id="content-placeholder">
			            	<div id="jqgrid">
			            		<span>
			                        <button id="btnAdd" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-plus"></span><span class="ui-button-text">Add</span></button>
			                    </span>
			                    <table id="grdPermission"></table>
			                    <div id="pagerPermission"></div>
			                 </div>
			                 <div>&nbsp;</div>
			                 <div>
			                	<button id="btnClear" type="reset" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"><span class="ui-button-icon-primary ui-icon ui-icon-refresh"></span><span class="ui-button-text">Clear</span></button>
			                 	<button id="btnSave" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-disk"></span><span class="ui-button-text">Save</span></button>
			             	</div>
			             </div>
			        </fieldset>            
			        	</div>
                      </div>
                  </div>
                </div>
            </div>
        </div>
</div>
    
</body>	
<%
		}else{
%>
<script type="text/javascript">
    	window.location.href = "EditProfile";
</script>
<%
		}
	}else{
%>
<script type="text/javascript">
	window.location.href = "LoginServlet";
</script>
<%
	}
	
%>
</html>